package by.dvrudenko.avtobaza.webapp.page.city;

import javax.inject.Inject;

import org.apache.wicket.authroles.authorization.strategies.role.annotations.AuthorizeInstantiation;
import org.apache.wicket.bean.validation.PropertyValidator;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.CompoundPropertyModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import by.dvrudenko.avtobaza.datamodel.City;
import by.dvrudenko.avtobaza.services.CityService;
import by.dvrudenko.avtobaza.webapp.page.BaseLayout;
import by.dvrudenko.avtobaza.webapp.page.home.HomePage;

@AuthorizeInstantiation({ "ADMIN" })
public class CreateCityPage extends BaseLayout {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(CreateCityPage.class);

	@Inject
	private CityService cityService;
	
	public CreateCityPage(final City city) {
		super();
		LOGGER.info("create page CreateCityPage");
		/*
		 * форма для создания нового города, необходимо ввести название города и координаты
		 * 
		  */
		Form<City> formCreateCity = new Form<City>("form", new CompoundPropertyModel<City>(city));
		
		final TextField<String> sityName = new TextField<String>("cityName");
		sityName.add(new PropertyValidator<String>());
		formCreateCity.add(sityName);
		
		final TextField<Double> sityCoordinateLng = new TextField<Double>("coordinateLng");
		sityCoordinateLng.add(new PropertyValidator<Double>());
		formCreateCity.add(sityCoordinateLng);
		
		final TextField<Double> sityCoordinateLat = new TextField<Double>("coordinateLat");	
		sityCoordinateLat.add(new PropertyValidator<Double>());
		formCreateCity.add(sityCoordinateLat);

		
		formCreateCity.add(new SubmitLink("sumbit-link") {
			@Override
			public void onSubmit() {
				super.onSubmit();
				cityService.saveOrUpdate(city);
				LOGGER.info(user.getLogin() + " add new City" + city.getCityName());
					setResponsePage(HomePage.class);
			}
		});
		
		add(formCreateCity);
	}



}
